/**
 * Created by ROGSTAR on 2016/11/7.
 */
require.config(window.jshead);
require(['jquery','layer','include/navigator'], function($,layer) {
    layer.config({
        path: '/js/layer/'
    });
    //图片验证码
    $('.validImg').click(function() {
        $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
    });

    $('#clickMes').bind('click',function(){
        var phone=$('#phone').val();
        var validNum=$("#code").val();
        if(phone==""){
            layer.tips("请先输入手机号",'.phoneNum');
            return;
        }
        time($('#clickMes'));

        $.ajax({
            type: "POST",
            url: "/backPasswordSms",
            data: {
                mobile:phone,
                pictureCode: validNum
            },
            dataType: "json",
            success: function (data) {

                if(data.result=="success"){
                    layer.msg("短信已发送！");
                }else{
                    wait=0;
                    layer.tips(data.messageText,'.login_input_code');
                }
            }

        });
    });
    var wait=60;
    function time(o) {
        if (wait == 0) {
            o.removeAttr("disabled");
            o.val('获取验证码');
            o.css("color", '#ffffff');
            o.css("background","#2c97f4");
            wait = 60;
            $('.validImg').trigger('click')
        } else {
            o.attr("disabled", true);
            o.css("color", '#fff');
            o.css("background", '#ddd');
            o.val("重新发送(" + wait + "s)");
            wait--;
            setTimeout(function() {
                    time(o)
                },
                1000)
        }
    }

    $('#forgetNext').bind('click',function(){
        var phone=$('#phone').val();
        var mobileCode=$('#mobileCode').val();
        var validNum=$("#code").val();
        if(phone==''){
            layer.tips("请先输入手机号",'.phoneNum');
            return;
        }
        if(mobileCode==''){
            layer.tips("请先进行手机验证",'.mobileCode');
            return;
        }
        if(validNum==""){
            layer.tips("请输入正确的验证码",'.login_input_code');
            return;
        }
        //图片验证
        $(".validImg").attr("src", "/sys/ImageServlet?time=" + new Date());
        $.ajax({
            type: "POST",
            url: "/forgetPassword/checkMobile",
            data: {
                mobile:phone,
                mobileCode:mobileCode,
                pictureCode: validNum
            },
            dataType: "json",
            success: function (data) {

                if(data.result=='success'){
                    $('#first').css('display','none');
                    $('#second').css('display','block');
                    $('#third').css('display','none');
                }else{
                    layer.msg(data.messageText);
                }
            }
        });
    });
    $("#mobileCode,#confirm").keydown(function() {
        if (event.keyCode == "13") {//keyCode=13是回车键
            $('#forgetNext,#forgetNext2').click();
        }
    });

    $('#forgetNext2').bind('click',function(){
        var phone=$('#phone').val();
        var password=$('#password').val();
        var confirm=$('#confirm').val();
        if(password==""){
            layer.tips("输入密码不能为空", '#password');
            return;
        }else if(password.length<6 || password.length>20){
            layer.tips("密码位数长度为6-20位", '#password');
            return;
        }
        if(confirm==""){
            layer.tips("输入密码不能为空", '#confirm');
            return;
        }
        if(password!=confirm) {
            layer.tips("两次输入密码不一致", '#confirm');
            return;
        }

        $.ajax({
            type: "POST",
            url: "/forgetPassword/doForgetPassword",
            data: {
                mobile:phone,
                password:password,
            },
            dataType: "json",
            success: function (data) {

                if(data.result=='success'){
                    $('#first').css('display','none');
                    $('#second').css('display','none');
                    $('#third').css('display','block');
                }else{
                    layer.msg(data.messageText);
                }
            }
        });
    })
});